import ToolActionApi from "@/tool/http/tool-action.js";
import toolListenCache from "./tool-listen-cache";
import DataListenConfig from "./components/data-listen-config.vue";
import { markRaw } from "vue";

let dataSourceAction = ToolActionApi.init("data_source");

let oldFormData = {};
let dataSourceDict = [];
let dataTableCache = [];
let dictRule = [];

let extendFields = [
  // {
  //   name: "listenDataSourceId",
  //   memo: "监听数据源",
  //   is: (watchForm) =>
  //     watchForm["dict_data_listen_type"] === "data-listen-database-mysql" &&
  //     dataSourceDict.length > 0,
  //   dictListFun: async () => dataSourceDict,
  // },
  // {
  //   name: "listenTableName",
  //   memo: "监听数据表",
  //   is: (watchForm) => watchForm.listenDataSourceId,
  //   dictListFun: async ({ listenDataSourceId }) => {
  //     if (oldFormData.listenDataSourceId === listenDataSourceId) {
  //       return dataTableCache;
  //     }
  //     oldFormData.listenDataSourceId = listenDataSourceId;
  //     let res = await ToolActionApi.run("/action-table-infos", "*", {
  //       _dsId: listenDataSourceId,
  //     });
  //     return (dataTableCache = res.map((item) => ({
  //       name: item.memo || item.name,
  //       val: item.name,
  //     })));
  //   },
  // },
  // {
  //   name: "listenDataKey",
  //   memo: "监听数据列",
  //   multiple: true,
  //   is: (watchForm) => watchForm.listenTableName,
  //   dictListFun: (watchForm) =>
  //     toolListenCache.dictTableCol(
  //       watchForm.listenDataSourceId,
  //       watchForm.listenTableName
  //     ),
  // },
  // {
  //   name: "tableWhereKey",
  //   memo: "监听条件列",
  //   is: (watchForm) => watchForm.listenTableName,
  //   dictListFun: (watchForm) =>
  //     toolListenCache.dictTableCol(
  //       watchForm.listenDataSourceId,
  //       watchForm.listenTableName
  //     ),
  // },
  // {
  //   name: "tableWhereRule",
  //   memo: "监听条件规则",
  //   is: (watchForm) => watchForm.listenTableName,
  //   dictListFun: () => dictRule,
  // },
  // {
  //   name: "tableWhereVal",
  //   memo: "监听条件值",
  //   is: (watchForm) => watchForm.listenTableName,
  // },
];

const rule = {
  queryParams: [],
  extendFields,
  metadata: [
    { name: "id" },
    {
      name: "extend_json",
      component: markRaw(DataListenConfig),
    },
  ],
};

export default async function () {
  (async function () {
    dataSourceDict = await dataSourceAction.dict();
    dictRule = await ToolActionApi.enumDict("data_rule");
  })();
  return rule;
}
